package main

import "fmt"

func main() {
	node := &TreeNode{
		Val:   1,
		Left:  nil,
		Right: nil,
	}
	tree := diameterOfBinaryTree(node)
	fmt.Println(tree)
}

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

var res *int

func diameterOfBinaryTree(root *TreeNode) int {
	_ = dfs(root)
	return *res
}

func dfs(node *TreeNode) int {
	if node == nil {
		return 0
	}
	l := dfs(node.Left)
	r := dfs(node.Right)
	*res = max(*res, l+r)
	return max(l, r) + 1
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
